/***********************************************
 * Title:       TeacherInfoManageAction.java
 * Description: TeacherInfoManageAction.java
 * Author:      NiDeveloper
 * Create Date: 2010-5-8
 * CopyRight:   CopyRight(c)@2009
 * Company:     TJUSCS
 * Version:     1.0
 ***********************************************
 */
package edu.tju.education;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.infohazard.maverick.flow.ControllerContext;

import com.younker.waf.maverick.DispatchCtl;

import edu.tju.education.model.TeacherInfo;
import edu.tju.teacherManagement.Teacher;

/**
 * @author NiDeveloper
 * 
 */
public class TeacherInfoManageAction extends DispatchCtl
{
        private Logger log = Logger.getLogger(TeacherInfoManageAction.class);
        private static String V_TEACHER_INFO = "teacherInfo";

        private TeacherInfo teacherInfo;
        private long teacherInfoId;
        private long teacherId;
        private Date gainDate;
        private String itemName;
        private String remark;
        private String state;
        private String teacherName;
        private Teacher teacher;
        private List<TeacherInfo> teacherInfoList;

        /*
         * (non-Javadoc)
         * 
         * @see
         * org.infohazard.maverick.ctl.FormBeanUser#makeFormBean(org.infohazard
         * .maverick.flow.ControllerContext)
         */
        @Override
        protected Object makeFormBean(ControllerContext arg0)
        {
                return new TeacherInfoManageAction();
        }

        public String displayTeacherInfo(Object beans, ControllerContext ctx)
        {
                TeacherInfoManageAction bean = (TeacherInfoManageAction) beans;

                long teacherId = bean.getTeacherId();

                Teacher teacher  = null;
                try
                {
                        teacher = (Teacher) sqlMap.queryForObject("getTeacherById", teacherId);
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                bean.setTeacher(teacher);
                
                List<TeacherInfo> teacherInfoList = null;
                try
                {
                        teacherInfoList = sqlMap.queryForList("getTeacherInfoByTeacherId", teacher.getId());
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

                Comparator<TeacherInfo> c = new Comparator<TeacherInfo>()
                {

                        @Override
                        public int compare(TeacherInfo o1, TeacherInfo o2)
                        {
                                if (o1.getGainDate().getTime() < o2.getGainDate().getTime())
                                        return 1;
                                if (o1.getGainDate().getTime() > o2.getGainDate().getTime())
                                        return -1;
                                return 0;

                        }

                };
                Collections.sort(teacherInfoList, c);
                
                bean.setTeacherInfoList(teacherInfoList);

                return V_TEACHER_INFO;

        }
        
        public String confirm( Object beans, ControllerContext ctx )
        {
                TeacherInfoManageAction bean = (TeacherInfoManageAction)beans;
                long teacherInfoId = bean.getTeacherInfoId();
                
                TeacherInfo teacherInfo = null;
                try
                {
                        teacherInfo = (TeacherInfo) sqlMap.queryForObject("getTeacherInfoById", teacherInfoId);
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
                teacherInfo.setId(teacherInfoId);
                teacherInfo.setState(TeacherInfo.TS_CONFIRMED);
                
                try
                {
                        sqlMap.update("updateTeacherInfo", teacherInfo);
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
                long teacherId = teacherInfo.getTeacherId();
                
                Teacher teacher  = null;
                try
                {
                        teacher = (Teacher) sqlMap.queryForObject("getTeacherById", teacherId);
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                bean.setTeacher(teacher);
                
                List<TeacherInfo> teacherInfoList = null;
                try
                {
                        teacherInfoList = sqlMap.queryForList("getTeacherInfoByTeacherId", teacher.getId());
                } catch (SQLException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

                Comparator<TeacherInfo> c = new Comparator<TeacherInfo>()
                {

                        @Override
                        public int compare(TeacherInfo o1, TeacherInfo o2)
                        {
                                if (o1.getGainDate().getTime() < o2.getGainDate().getTime())
                                        return 1;
                                if (o1.getGainDate().getTime() > o2.getGainDate().getTime())
                                        return -1;
                                return 0;

                        }

                };
                Collections.sort(teacherInfoList, c);
                
                bean.setTeacherInfoList(teacherInfoList);
                
                return V_TEACHER_INFO;
        }

        /**
         * @return the teacherInfoId
         */
        public long getTeacherInfoId()
        {
                return teacherInfoId;
        }

        /**
         * @param teacherInfoId
         *                the teacherInfoId to set
         */
        public void setTeacherInfoId(long teacherInfoId)
        {
                this.teacherInfoId = teacherInfoId;
        }

        /**
         * @return the teacherId
         */
        public long getTeacherId()
        {
                return teacherId;
        }

        /**
         * @param teacherId
         *                the teacherId to set
         */
        public void setTeacherId(long teacherId)
        {
                this.teacherId = teacherId;
        }

        /**
         * @return the gainDate
         */
        public Date getGainDate()
        {
                return gainDate;
        }

        /**
         * @param gainDate
         *                the gainDate to set
         */
        public void setGainDate(Date gainDate)
        {
                this.gainDate = gainDate;
        }

        /**
         * @return the itemName
         */
        public String getItemName()
        {
                return itemName;
        }

        /**
         * @param itemName
         *                the itemName to set
         */
        public void setItemName(String itemName)
        {
                this.itemName = itemName;
        }

        /**
         * @return the remark
         */
        public String getRemark()
        {
                return remark;
        }

        /**
         * @param remark
         *                the remark to set
         */
        public void setRemark(String remark)
        {
                this.remark = remark;
        }

        /**
         * @return the state
         */
        public String getState()
        {
                return state;
        }

        /**
         * @param state
         *                the state to set
         */
        public void setState(String state)
        {
                this.state = state;
        }

        /**
         * @return the teacherInfo
         */
        public TeacherInfo getTeacherInfo()
        {
                return teacherInfo;
        }

        /**
         * @param teacherInfo
         *                the teacherInfo to set
         */
        public void setTeacherInfo(TeacherInfo teacherInfo)
        {
                this.teacherInfo = teacherInfo;
        }

        /**
         * @return the teacherName
         */
        public String getTeacherName()
        {
                return teacherName;
        }

        /**
         * @param teacherName
         *                the teacherName to set
         */
        public void setTeacherName(String teacherName)
        {
                this.teacherName = teacherName;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString()
        {
                return "TeacherInfoManageAction [gainDate=" + gainDate + ", itemName=" + itemName
                                + ", remark=" + remark + ", state=" + state + ", teacherId="
                                + teacherId + ", teacherInfo=" + teacherInfo + ", teacherInfoId="
                                + teacherInfoId + ", teacherName=" + teacherName + "]";
        }

        /**
         * @return the teacher
         */
        public Teacher getTeacher()
        {
                return teacher;
        }

        /**
         * @param teacher the teacher to set
         */
        public void setTeacher(Teacher teacher)
        {
                this.teacher = teacher;
        }

        /**
         * @return the teacherInfoList
         */
        public List<TeacherInfo> getTeacherInfoList()
        {
                return teacherInfoList;
        }

        /**
         * @param teacherInfoList the teacherInfoList to set
         */
        public void setTeacherInfoList(List<TeacherInfo> teacherInfoList)
        {
                this.teacherInfoList = teacherInfoList;
        }

}
